package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.lawEnforcing.impl.dao.CasesInformationDao;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.CasesInformation;
import com.jiantong.lawEnforcing.service.CasesInformationService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 案件信息表
 * Created by mz03 on 2017/9/13.
 */
@Slf4j
@Service
@RpcProvider
public class CasesInformationServiceImpl implements CasesInformationService {
    private final CasesInformationDao casesInformationDao;

    public CasesInformationServiceImpl(CasesInformationDao casesInformationDao) {
        this.casesInformationDao = casesInformationDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return News
     */
    @Override
    public Response<CasesInformation> finById(Long id) {
        try {
            CasesInformation casesInformation = casesInformationDao.findById(id);
            if (casesInformation == null) {
                log.error("casesInformation (id={}) is not found", id);
                return Response.fail("根据id查询案件信息失败");
            }
            return Response.ok(casesInformation);
        } catch (Exception e) {
            log.error("failed to find casesInformation(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("casesInformation.find.fail");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param casesInformation 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(CasesInformation casesInformation) {
        try {
            //生成案件编号
            String cases_number = "LAW-"+ new Date().getTime();
            casesInformation.setCases_number(cases_number);

            //设置案件办理人id: receiver_id
            casesInformation.setReceiver_id(UserUtil.getUserId());

            Boolean b = casesInformationDao.create(casesInformation);
            if (b) {
                return Response.ok(casesInformation.getId());
            }
            return Response.fail("创建casesInformation失败");
        } catch (Exception e) {
            log.error("failed.to.create.casesInformation:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.casesInformation.failed");
        }

    }

    /**
     * 更新新闻的单据
     *
     * @param casesInformation 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(CasesInformation casesInformation) {
        try{
            return Response.ok(casesInformationDao.update(casesInformation));
        }catch (Exception e){
            log.error("failed.to.update.casesInformation:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.casesInformation.failed");
        }
    }

    /**
     *
     * @param pageNo
     * @param pageSize
     * @param criteria
     * @return
     */
    @Override
    public Response<Paging<CasesInformation>> pagingByStatus(Integer pageNo, Integer pageSize,Map<String,Object> criteria) {
        try{
            return Response.ok(casesInformationDao.pagingList(pageNo,pageSize,criteria));
        }catch (Exception e){
            log.error("failed to find CasesInformation, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("CasesInformation.find.fail");
        }
    }

    /**
     * 根据一组status分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param statusList
     * @param casesInformation
     * @return
     */
    @Override
    public Response<Paging<CasesInformation>> pagingByCasesInformation(Integer pageNo, Integer pageSize, List<Integer> statusList, CasesInformation casesInformation) {
        return null;
    }

    @Override
    public Response<List<Map<String,Integer>>> countByCasesSource() {
        try{
            return Response.ok(casesInformationDao.countByCasesSource());
        }catch (Exception e){
            log.error("failed to find countByCasesSource, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("countByCasesSource.find.fail");
        }
    }

    /**
     * 案件类型占比图
     *
     * @return
     */
    @Override
    public Response<List<Map<String, Integer>>> countByCasesType() {
        try{
            return Response.ok(casesInformationDao.countByCasesType());
        }catch (Exception e){
            log.error("failed to find countByCasesType, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("countByCasesType.find.fail");
        }
    }
}
